import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    keepAliveList: [], //保存缓存的列表
    keepAliveExcludList: [] //不保存的列表
  },
  mutations: {
    keepAliveListAdd(state, newComponentName) {
      // console.log("keep******", state.keepAliveList);
      // console.log("keepExclud******", state.keepAliveExcludList);
      // 注：防止重复添加（当然也可以使用Set）
      !state.keepAliveList.includes(newComponentName) &&
        state.keepAliveList.push(newComponentName);

      const index = state.keepAliveExcludList.indexOf(newComponentName);
      index !== -1 && state.keepAliveExcludList.splice(index, 1);
    },
    keepAliveListRemove(state, componentName) {
      const index = state.keepAliveList.indexOf(componentName);
      index !== -1 && state.keepAliveList.splice(index, 1);

      !state.keepAliveExcludList.includes(componentName) &&
        state.keepAliveExcludList.push(componentName);
    }
  },
  actions: {},
  modules: {}
});
